#define TEST

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using BoOnce.Components;
using BoOnce.Xml;
using BoOnce.Graphics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using BoOnce.GameLogic;
using System.Timers;
using BoOnce.Test;
using BoOnce.GameLogic.Entities;
using BoOnce.Steps;
using BoOnce.Helpers;
using BoOnce.Core.Components;
using BoOnce.Core.Helpers;

namespace BoOnce
{

    public class BaseGame : Microsoft.Xna.Framework.Game
    {
        private static Game _Game;
        private GraphicsDeviceManager _Graphics;

        private static Boolean _IsPause;

        public static Boolean Pause
        {
            get
            {
                return _IsPause;
            }
            set
            {
                _IsPause = value;
                BackgroundTaskComponent btc = (BackgroundTaskComponent)_Game.Services.GetService(typeof(BackgroundTaskComponent));
                DrawableEntityComponent dec = (DrawableEntityComponent)_Game.Services.GetService(typeof(DrawableEntityComponent));
                PlayerActionComponent pac = (PlayerActionComponent)_Game.Services.GetService(typeof(PlayerActionComponent));
                CameraComponent cc = (CameraComponent)_Game.Services.GetService(typeof(CameraComponent));

                if (_IsPause)
                {
                    if(btc!=null) btc.Enabled = false;
                    if (dec != null) dec.Enabled = false;
                    if (pac != null) pac.Enabled = false;
                    if (cc != null) cc.Enabled = false;
                }
                else
                {
                    if (btc != null) btc.Enabled = true;
                    if (dec != null) dec.Enabled = true;
                    if (pac != null) pac.Enabled = true;
                    if (cc != null) cc.Enabled = true;
                }
            }
        }

        public BaseGame()
        {
            _Graphics = new GraphicsDeviceManager(this);
            this.Services.AddService(typeof(GraphicsDeviceManager), _Graphics);
            Content.RootDirectory = Settings.CONTENT_ROOT_PATH;
            _Game = this;
        }

        public static Game GetInstance()
        {
            return _Game;
        }


        protected override void Initialize()
        {
            Pause = false;
            Logger.init();

        #if DEBUG
            //XmlStorage.Save("Config/keyboard.xml", new KeyboardXml());
            //XmlStorage.Save("Config/gamepad.xml", new GamepadXml());
            //XmlStorage.Save("somebodyExample.xml", new SomebodyDescriptionXml());
            //XmlStorage.Save("levelExample.xml", new LevelXml());
            IsMouseVisible = true;
        #endif

            _Graphics.PreferredBackBufferHeight = Settings.SCREEN_Y;
            _Graphics.PreferredBackBufferWidth = Settings.SCREEN_X;
            _Graphics.IsFullScreen = Settings.SCREEN_FULLSCREEN;
            _Graphics.ApplyChanges();

            Logger.WriteInfo("Max Texture Height:" +_Graphics.GraphicsDevice.GraphicsDeviceCapabilities.MaxTextureHeight);
            Logger.WriteInfo("Max Texture Width:" + _Graphics.GraphicsDevice.GraphicsDeviceCapabilities.MaxTextureWidth);


            //VideoContentManager _VideoContentManager = new VideoContentManager(Services);
            //_VideoContentManager.RootDirectory = Settings.CONTENT_ROOT_PATH;

            InputComponent _InputComponent = new InputComponent(this,Settings.CONTENT_ROOT_PATH + "/" +Settings.CONTENT_CONFIG_PATH+"/keyboard.xml");
            _InputComponent.UpdateOrder = Priority.INPUT_COMPONENT;
            _InputComponent.Enabled = true;
            
            StepTransitionComponent _StepTransitionComponent = new StepTransitionComponent(this);
            _StepTransitionComponent.Enabled = true;
            _StepTransitionComponent.UpdateOrder = 0;
            _StepTransitionComponent.DrawOrder = 0;

            PauseComponent _PauseComponent = new PauseComponent(this);
            _PauseComponent.Enabled = true;
            _PauseComponent.DrawOrder = Priority.LAST;

            _Game.Services.AddService(typeof(Game), this);
            _Game.Services.AddService(typeof(InputComponent), _InputComponent);
            _Game.Services.AddService(typeof(StepTransitionComponent), _StepTransitionComponent);
            //_Game.Services.AddService(typeof(VideoContentManager), _VideoContentManager);
            _Game.Services.AddService(typeof(PauseComponent), _PauseComponent);

            _Game.Components.Add(_InputComponent);
            _Game.Components.Add(_StepTransitionComponent);
            _Game.Components.Add(_PauseComponent);

            _StepTransitionComponent.GoTo(new InitGameStep(this));

            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }


        protected override void UnloadContent()
        {
            base.UnloadContent();
        }


        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            if (Keyboard.GetState().IsKeyDown(Keys.P))
                Pause = !Pause;

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            _Graphics.GraphicsDevice.Clear(Color.Gray);
            base.Draw(gameTime);
        }
    }
}
